package com.sx.core.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.CompressionCodecs;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.xml.bind.DatatypeConverter;
import java.util.Base64;
import java.util.Date;

/**
 * Created by lihao on 2018/1/15.
 */
public class JwtUtils {
    static private Log log= LogFactory.getLog(JwtUtils.class);
    static String jwtSecretKey="c3NRV1FXUVdRV1dRVw==";
    static long period=1800000;
    public static void main(String[] args){

//        Information information=new Information();
//        information.setId("010dc88b4742499fa2b3a9ece7c81066");
//        information.setName("d");
//
//        String token=createJWT("d", JSON.toJSONString(information));
//        System.out.println(token);
//        parseJWT(token);



    }
    /**
     * JWT签发令牌
     *
     * @param id token id
     * @param subject 用户ID
     * @return json web token
     */
    public static String createJWT(String id,String subject) {

        SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
        // 当前时间戳(精确到毫秒)
        long currentTimeMillis = System.currentTimeMillis();
        Date now = new Date(currentTimeMillis);
        // 秘钥
        byte[] secretKeyBytes = Base64.getDecoder().decode(jwtSecretKey);
        JwtBuilder jwt  =  Jwts.builder();
        jwt.setId(id);
        // 用户名
        jwt.setSubject(subject);
        // 签发者
        jwt.setIssuer("one-card");
        // 签发时间
        jwt.setIssuedAt(new Date(currentTimeMillis));
        // 有效时间
        Date expiration = new Date(currentTimeMillis+period*1000);
        jwt.setExpiration(expiration).setNotBefore(now);
        // 访问主张-角色
//        if(null!=roles&&!"".equals(roles)) jwt.claim("roles", roles);
        // 访问主张-权限
//        if(null!=permissions&&permissions.size()>0) jwt.claim("perms", permissions);
        jwt.compressWith(CompressionCodecs.DEFLATE).signWith(signatureAlgorithm, secretKeyBytes);
        return jwt.compact();
    }
    public static String createJWTObj(String id,String subject) {

        SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
        // 当前时间戳(精确到毫秒)
        long currentTimeMillis = System.currentTimeMillis();
        Date now = new Date(currentTimeMillis);
        // 秘钥
        byte[] secretKeyBytes = Base64.getDecoder().decode(jwtSecretKey);
        JwtBuilder jwt  =  Jwts.builder();
        jwt.setId(id);
        // 用户名
        jwt.setSubject(subject);
        // 签发者
        jwt.setIssuer("iisp");
        // 签发时间
        jwt.setIssuedAt(new Date(currentTimeMillis));
        // 有效时间
        Date expiration = new Date(currentTimeMillis+period*1000);
        jwt.setExpiration(expiration).setNotBefore(now);
        // 访问主张-角色
//        if(null!=roles&&!"".equals(roles)) jwt.claim("roles", roles);
        // 访问主张-权限
//        if(null!=permissions&&permissions.size()>0) jwt.claim("perms", permissions);
        jwt.compressWith(CompressionCodecs.DEFLATE).signWith(signatureAlgorithm, secretKeyBytes);
        return jwt.compact();
    }

    public static Claims parseJWT(String jsonWebToken){
        try
        {
            Claims claims = Jwts.parser()
                    .setSigningKey(DatatypeConverter.parseBase64Binary(jwtSecretKey))
                    .parseClaimsJws(jsonWebToken).getBody();
            return claims;
        }
        catch(Exception ex)
        {
            log.error("parseJWT",ex);

            return null;
        }
    }
}
